<?php
/** 
 * 控制器类文件(首页模块，文章模块)
 * 
 * 文章的操作
 * @author      sinalma  <sinalma@foxmail.com>
 * @version     1.0 版本号
 */ 
namespace app\index\controller;

use think\Controller;
use think\View;
use think\Db;
use app\index\model\Article;
use app\common\tools\Pagintor;
use think\Session;
use app\common\tools\QRCode;
use think\Route;
use app\common\controller\MarkDown;



/** 
 * Class Index
 * 文章模型类 
 *
 * @package app\index\controller
 * @author      sinalma  <sinalma@foxmail.com>
 * @version     1.0 版本号
 */ 
class Index extends Controller
{

    /**  
     * 解析markdown格式文本的测试方法
     * not use
     * 
     * @access public 
     * @return NULL
     */
    public function parse(){
        $text =  "mmm![wamp_php01.png](https://upload-images.jianshu.io/upload_images/1882592-a9b9327511b0cae2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)mmdfgsdfgmdfgdfgmmm![wamp_php0.png](https://upload-images.jianshu.io/upload_images/1882592-a9b9327511b0cae2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)mmm";
        // 图片标记的匹配替换
        // $pattern = '/^!\[\.*\](\(\s+\))$/';
        $pattern = '/\!\[.*\]\((.*)\)$/';
        $matches = array();
        $count = preg_match_all($pattern, $text,$matches);
        echo $count;
        var_dump($matches);die;
        if (!$count)return;
        $count = preg_replace($pattern,"图$1片",$text,2,$n);
        echo $count."=".$n;
    }


    /**  
     * 获取指定cid的文章详情页页面
     * 
     * @access public 
     * @param cid get参数
     * @return View
     */
    public  function article(){
        $slug = input('param.slug');
        $artObj = new Article();
        $res = $artObj->getArt($slug);
        $user = Article::get($res['cid']);
        $user->order = $user->order+1;
        // 显式指定当前操作为新增操作
        $user->isUpdate(true)->save();
        $text = $res['text'];
        $text = str_replace('<!--markdown-->','',$text);
        $parse = new MarkDown();
        $text = $parse->makeHtml($text);
        $this->assign('art',$res);
        $this->assign('text',$text);
        $bet = $artObj->getBetween($res['cid']);
        $this->assign('lastArt',$bet[0]);
        $this->assign('nextArt',$bet[1]);
        // 生成二维码
        // $artUrl = "http://www.sinalma.cn/article/{$res['slug']}";
        // $img = QRCode::create($artUrl);
        // file_put_contents('http://www.sinalma.cn/public/static/upload/article/qrcode/'.$res['slug'].'.png',$img);
        // $this->assign('qrcode',"http://www.sinalma.cn/public/static/upload/article/qrcode/{$res['slug']}.png");
        $this->assign('qrcode','http://www.sinalma.cn/public/static/uploads/article/qrcode/art.png');
        return $this->fetch();
    }

    

    /**  
     * 火拳指定cid的文章详情页页面
     * 
     * @access public 
     * @param cid get参数
     * @return View
     */
    public  function article2(){
        // $cid = input('param.cid');
        $slug = input('param.slug');
        $artObj = new Article();
        $res = $artObj->getArt($slug);
        $user = Article::get($res['cid']);
        $user->order = $user->order+1;
        // 显式指定当前操作为新增操作
        $user->isUpdate(true)->save();
        $text = $res['text'];
        // $text =  str_replace(array("\r\n", "\r", "\n"),'\n',$text);
        // $text =  str_replace("\r\n",'\r\n',$text);
        // $text =  str_replace("\r",'\r',$text);
        // $text =  str_replace("\n",'\n',$text);

        // ![wamp_php01.png](https://upload-images.jianshu.io/upload_images/1882592-a9b9327511b0cae2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        // 图片标记的匹配替换
        //$pattern = '/\![+\s\.png](\(+\s\))/';
        //$text = preg_replace($pattern,'<a>$1</a>',$text);
        // $text = $this->parse($text);
        $res['text'] = $text;
        $this->assign('art',$res);
        return $this->fetch();
    }



    /**  
     * 加载留言视图
     * 
     * @access public 
     * @return view 
     */
   public function messages(){
       return $this->fetch();
   }


   
    /**  
     * 获取搜索数据
     * 
     * @access public 
     * @return string 
     */   
   public function search(){
        $pointStr = input("param.pointStr");
        $this->assign('pointStr',$pointStr);
        return $this->fetch();
   }


    /**  
     * 获取指定作者下的缩略文章
     * 
     * @access public 
     * @param author get参数
     * @return View
     */
   public function author(){
       $author = input('param.author');
       $this->assign('author',$author);
       return $this->fetch();
   }


    /**  
     * 获取指定作者下的所有文章数据,缩略信息
     * 
     * @access public 
     * @param author get参数
     * @param page get参数
     * @return html
     */
   public function getAuthorArts(){
        $author = input("param.author");
        $page = input("param.page");
        $artObj = new Article();
        $artCount = $artObj->getAuthorCount($author);
        $pageObj = new Pagintor($artCount,10);
        $limitStr = $pageObj->limit;
        $artHtml = $artObj->getAuthorArticles($author,$limitStr,10);
        $pageHtml = $pageObj->fnumpage();
        $artHtml .= $pageHtml;
        return $artHtml;
   }


    /**  
     * 根据关键字获取包含的标题文章,缩略信息
     * 
     * @access public 
     * @param pointStr get参数
     * @param page get参数
     * @return html
     */
   public function getSearchArts(){
       $pointStr = input("param.pointStr");
       $page = input("param.page");
       $pageNum = 10;
       // $limitStr = "limit ".($page-1)*$pageNum.",".$pageNum;
       $art = new Article();
       $html = $art->getSearch($pointStr,$page,$pageNum);
       return $html;
   } 


    /**  
     * 控制器显示页面方法(所有文章分类)
     * 文章分类页面
     * 
     * @access public 
     * @return array 
     */     
    public function categories(){
        return $this->fetch();
    }


    /**  
     * 获取分类信息(所有文章分类)
     * 不加载页面
     * 
     * @access public 
     * @return array 
     */         
    public function getCategories(){
        $art = new Article();
        $html = $art->getCategories();
        return $html;
    }

    /**  
     * 控制器显示页面方法(所有文章标签)
     * 文章标签页面
     * 
     * @access public 
     * @return array 
     */  
    public function tag(){
        $this->meta("tag");
        return $this->fetch();
    }


    /**  
     * 控制器显示页面方法(指定分类)
     * 
     * @access public 
     * @return array 
     */  
    public function category(){
        $this->meta("category");
        return $this->fetch();
    }


    /**  
     * 获取(分类/标签)数据
     * 
     * @param mixed $type 分类或者标签('category'/'tag')
     * @access public 
     */      
    public function meta($type){
        $slug = input("param.slug");
        $this->assign($type."Slug",$slug);
        $art = new Article();
        $name = $art->getMetaName($slug);
        $this->assign($type."Name",$name);
    }



    /**  
     * 获取分类/标签下的文章
     * 
     * @param mixed $type 分类或者标签('category'/'tag')
     * @access public 
     */         
    public function getCategoryArticles(){
        return $this->getMetaArticles("category");
    }
    public function getTagArticles(){
        return $this->getMetaArticles("tag");
    }
    public function getMetaArticles($type){
         // 获取当前action (categories,tags)
        // $action = request()->action();
        // $page = !empty(input('param.page')) ? input('param.page') : 0;
        $slug = input("param.slug");
        $artObj = new Article();
        $artCount = $artObj->getMetaCount($slug);
        $pageObj = new Pagintor($artCount,5);
        $limitStr = $pageObj->limit;
        $artHtml = $artObj->getMetaArticles($slug,$limitStr,5);
        $pageHtml = $pageObj->fnumpage();
        $artHtml .= $pageHtml;
        return $artHtml;
    }


    /**  
     * 控制器显示页面方法
     * 标签页
     *
     * @access public 
     * @return view  
     */       
    public function tags(){
        return $this->fetch();
    }


    /**  
     * 获取分类信息
     * 不加载页面
     *
     * @access public 
     * @return string      
     */        
    public function getTags(){
        $art = new Article();
        $html = $art->getTags();
        return $html;
    }
    

    /**  
     * 链接模块
     *
     * @access public 
     * @return view      
     */           
    function links(){
        return $this->fetch();
    }

    
    /**  
     * 简介模块
     *
     * @access public 
     * @return view      
     */        
    function about(){
        return $this->fetch();
    }


    /**  
     * 首页，显示博客模块
     *
     * @access public 
     * @return view      
     */         
    public function index()
    {
        $user = Session::get("user");
        if ($user['user_name']){
            $this->assign("username",$user['user_name']);
        }else{
            $this->assign("username",'');
        }
        return $this->fetch();
    }


    /**  
     * 首页，显示博客模块
     * not use
     *
     * @access public 
     * @return view      
     */         
    public function getArts(){
        // 获取当前action (categories,tags)
        $action = request()->action();
        // $page = !empty(input('param.page')) ? input('param.page') : 0;

        $artObj = new Article();
        $artCount = $artObj->getMetaCount();
        $pageObj = new Pagintor($artCount,5);
        $limitStr = $pageObj->limit;
        $artHtml = $artObj->getMetaArticles($limitStr,5);
        $pageHtml = $pageObj->fnumpage();
        $artHtml .= $pageHtml;
        return $artHtml;
        
    }



    /**  
     * 首页
     * 获取一页博客，需要参数page，如不传，默认为1
     * by @sinalma 2018.07.24 00.03
     * 
     * @param mixed $type 分类或者标签('category'/'tag')
     * @access public 
     */           
    public function getArticles(){
        $id = input('param.page');
        // 实例化文章模型
        $artObj = new Article();
        // 获取总文章数
        $artCount = $artObj->getCount();
        // 实例化分页对象
        $pageObj = new Pagintor($artCount,10);
        // 获取当前页limit限制语句
        $limitStr = $pageObj->limit;
        // 获取html数据
        $artHtml = $artObj->getArticles($limitStr,10);
        $pageHtml = $pageObj->fnumpage();
        $artHtml .= $pageHtml;
        return $artHtml;
    }
}
